﻿using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Timers;
using RMA.Rhino;
using RMA.OpenNURBS;
using PubSubSubscriber;

namespace ResponsiveRhino
{
    public partial class Rhino_IO_UI : Form
    {
        private IRhinoCommandContext context;

        private Subscriber sub;
        private string msgReceived;
        //a list of subscribed topic as key and the associated point
        public Dictionary<string, double> topicList;

        //private RhinoIOData data;

        public Rhino_IO_UI()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
            topicList = new Dictionary<string, double>();
            sub = new Subscriber("127.0.0.1", 10009);
            sub.messageChanged += UpdateMessage;
        }

        public Rhino_IO_UI(IRhinoCommandContext context)
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;            
            topicList = new Dictionary<string, double>();
            sub = new Subscriber("127.0.0.1", 10009);
            sub.messageChanged += UpdateRhino;
            this.context = context;
        }
        
        private void btnSubscribe_Click(object sender, EventArgs e)
        {
            Debug.WriteLine("Subscribe to " + txtSubscribe.Text);
            Debug.Flush();
            sub.Subscribe(txtSubscribe.Text);

            if (topicList != null)
                topicList.Add(txtSubscribe.Text, 0);

            //if (data != null)
            //{
            //    Dictionary<string, double> list = data.TopicList;
            //    list.Add(txtSubscribe.Text, 0);
            //    data.TopicList = list;
            //}

            lstTopic.Items.Add(txtSubscribe.Text);
         }

        private void btnUnsubscribe_Click(object sender, EventArgs e)
        {
            Debug.WriteLine("Unsubscribe " + txtSubscribe.Text);
            Debug.Flush();
            sub.Unsubscribe(txtSubscribe.Text);
            if (topicList != null)
                topicList.Remove(txtSubscribe.Text);
            //if (data != null)
            //{
            //    Dictionary<string, double> list = data.TopicList;
            //    list.Remove(txtSubscribe.Text);
            //    data.TopicList = list;
            //}
            lstTopic.Items.Remove(txtSubscribe.Text);
        }

        private void UpdateMessage(object sender, MessageChangedArgs args)
        {
            try
            {
                msgReceived = args.message;
                this.txtInMessages.AppendText(msgReceived + Environment.NewLine);
                string[] str = msgReceived.Split(',');

                //topicList[str[0]] = Convert.ToDouble(str[1]) / 100;
                topicList[str[0]] = Convert.ToDouble(str[1]);
            }
            catch (Exception e)
            {
                Debug.WriteLine("Exception : " + e.Message);
                Debug.WriteLine(e.StackTrace);
                Debug.Flush();
            }
        }

        private void UpdateRhino(object sender, MessageChangedArgs args)
        {
            try
            {
                msgReceived = args.message;
                this.txtInMessages.AppendText(msgReceived + Environment.NewLine);
                string[] str = msgReceived.Split(',');
                                
                //topicList[str[0]] = Convert.ToDouble(str[1]) / 100;
                topicList[str[0]] = Convert.ToDouble(str[1]);
                
                On3dPoint p = new On3dPoint(topicList[str[0]], 0, 0);
                //Drawing points
                System.Drawing.Color red = System.Drawing.Color.Red;
                OnColor pointCol = new OnColor(ref red);
                On3dmObjectAttributes att = new On3dmObjectAttributes();
                context.m_doc.GetDefaultObjectAttributes(ref att);
                att.m_color = pointCol;
                att.SetColorSource(IOn.object_color_source.color_from_object);
                context.m_doc.AddPointObject(p, att);

                System.Drawing.Color blue = System.Drawing.Color.Blue;
                OnColor lineCol = new OnColor(ref blue);
                OnLine line = new OnLine(new On3dPoint(0, 0, 0), p);
                att.m_color = lineCol;
                att.SetColorSource(IOn.object_color_source.color_from_object);
                context.m_doc.AddCurveObject(line, att);

                context.m_doc.Redraw();
            }
            catch (Exception e)
            {
                Debug.WriteLine("Exception : " + e.Message);
                Debug.WriteLine(e.StackTrace);
                Debug.Flush();
            }
        }

    }
}
